package com.kongyz.config;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

import java.io.IOException;

/**
 * @author kongyz
 * @date 2018/7/19 22:17
 * @Description:
 */
public class ConfigWather implements Watcher {
    private ActiveKeyValueStore store;

    public ConfigWather(String host) {
        store=new ActiveKeyValueStore();
        try {
            store.connect(host);
            Thread.sleep(100);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if(watchedEvent.getType()==Event.EventType.NodeDataChanged){
            displayConfig();
        }
    }

    public void displayConfig(){
        String value=store.read(ConfigUpdater.PATH,this);
        System.out.printf("Read %s as %s \n",ConfigUpdater.PATH,value);
    }

    public static void main(String[] args) throws InterruptedException {
        ConfigWather configWather=new ConfigWather("192.168.88.66");
        configWather.displayConfig();

        Thread.sleep(Long.MAX_VALUE);
    }

}
